import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class Provider
{
    // date format for our times
    private static SimpleDateFormat dateFormat;
    static
    {
        // hour: minute AM/PM
        dateFormat = new SimpleDateFormat("h:mm a");
        
        // time zone GMT for simple times since we aren't using date
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    }
    
    private String name; // name of provider
    private Place place; // place provider is at
    private int capacity; // capacity provider can handle
    private Time open, // open time of provider
            close; // closing time of provider
            
    /**
     * Initialize the provider
     * 
     * @param name
     *            name of provider
     * @param place
     *            place provider is at
     * @param capacity
     *            capacity provider can handle
     * @param timeOpen
     *            open time of provider
     * @param timeClose
     *            closing time of provider
     */
    public Provider(String name, Place place, int capacity, Time timeOpen, Time timeClose)
    {
        setName(name);
        setPlace(place);
        setCapacity(capacity);
        setOpenTime(timeOpen);
        setCloseTime(timeClose);
    }
    
    /**
     * @return name of provider
     */
    public String getName()
    {
        return name;
    }
    
    /**
     * @return place of provider
     */
    public Place getPlace()
    {
        return place;
    }
    
    /**
     * @return capacity provider can handle
     */
    public int getCapacity()
    {
        return capacity;
    }
    
    /**
     * @return open time of provider
     */
    public Time getOpenTime()
    {
        return open;
    }
    
    /**
     * @return close time of provider
     */
    public Time getCloseTime()
    {
        return close;
    }
    
    /**
     * Set provider's name
     * 
     * @param name
     *            name of provider
     */
    private void setName(String name)
    {
        this.name = name;
    }
    
    /**
     * Set place provider is at
     * 
     * @param place
     *            place provider is at
     */
    private void setPlace(Place p)
    {
        this.place = p;
    }
    
    /**
     * Set capacity provider can handle
     * 
     * @param capacity
     *            capacity provider can handle
     */
    private void setCapacity(int cap)
    {
        this.capacity = cap;
    }
    
    /**
     * Set provider's open time
     * 
     * @param open
     *            open time of provider
     */
    private void setOpenTime(Time timeOpen)
    {
        this.open = timeOpen;
    }
    
    /**
     * Set provider's close time
     * 
     * @param close
     *            close time of provider
     */
    private void setCloseTime(Time timeClose)
    {
        this.close = timeClose;
    }
    
    /**
     * Check if provider is open at a given time
     * 
     * @param time
     *            time to check if provider is open at
     * @return if provider is open at given time
     */
    public boolean isOpen(Time time)
    {
        // true if open and close are same(always open) or time is between open and close
        return (open.equals(close)) || ((time.after(open) || time.equals(open)) && (time.before(close) || time.equals(close)));
        
        /*
         * The following would be needed if close could be earlier than open
         * but we will instead just require close is after open.
         * 
         * NOTE: This means that if a provider opened at 7AM and closed at 1AM,
         * close time would be 25 hours, not 1 hour.
         * 
         * Remove this before release it we keep this way of doing it
         */

        /*
         * // close time is after open time(like open @ 1PM close @ 7PM)
         * if (open.before(close))
         * return time.after(open) && time.before(close);
         * // closes before open time(like open @ 7AM, close at 1AM)
         * else
         * return time.after(close) || time.before(open);
         */
    }
    
    /**
     * @return String representation of the provider
     */
    public String toString()
    {
        return String.format("%-10s @ %s, Cap: %s\n\t\tOpen %s - %s", name, place.toString(), capacity, dateFormat.format(open), dateFormat.format(close));
    }
}
